<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09-filter过滤器</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<!--1.准备一个div容器-->
<div id="app">
<p>性别：{{sex|sexFilter}}</p>
<p>价格：{{price|priceFilter}}</p>
<p>生日：{{birthday|dateFilter}}</p>
<p></p>
</div>
<div id="app1">
    <p>性别：{{sex|sexFilter}}</p>
    <p>价格：{{price|priceFilter}}</p>
    <p>生日：{{birthday|dateFilter}}</p>
    <p></p>
</div>
<script>
    // 全局过滤器
    Vue.filter("sexFilter",function (sex) {
        return sex==1?"man":"woman";
    })
    Vue.filter("priceFilter",function (price) {
        let s = price+"";
        let index = s.indexOf(".");
        s=s.substring(0,index+3);
        return s;
    })
    Vue.filter("dateFilter",function (date) {
        let year = date.getFullYear();
        let month = date.getMonth();
        let day = date.getDate();
        return year+"年"+(month+1)+"月"+day+"日";
    })

    //2.准备一个Vue对象和容器绑定
    const vm = new Vue({
        el: "#app",
        data: {
            sex:1,
            price:139.999999,
            birthday:new Date(),
        },
        methods: {},
        filters:{// 局部过滤器
            // 性别过滤器
            sexFilter:function (sex) {
                return sex==1?"男":"女";
            },
            // 价格过滤器
            priceFilter:function (price) {
                let s = price+"";
                let index = s.indexOf(".");
                s=s.substring(0,index+3);
                return s;
            },
            // 日期过滤器
            dateFilter:function (date) {
                let year = date.getFullYear();
                let month = date.getMonth();
                let day = date.getDate();
                return year+"年"+(month+1)+"月"+day+"日";
            }
        }
    });
    const vm1 = new Vue({
        el: "#app1",
        data: {
            sex:0,
            price:139.999999,
            birthday:new Date(),
        },
        methods: {},
        filters:{
        }
    });
</script>
</body>
</html>